<?php

class Admin_LopController extends ExtLb_Controller_Action
{
	private $lopTbl;
	private $manager;
	
	private $lopForm;
	
	private $auth;
	private $storage;
	
    public function init()
    {
        $this->loadLayout('admin', 'layout');
        $this->loadCss('admin', 'lop');
        
        $this->lopForm 	= new Admin_Form_LopForm();
        $this->lopTbl 	= new Admin_Model_Lops();
        $this->manager	= new Admin_Model_Manages();
        
        $this->auth = Zend_Auth::getInstance();
        $this->storage = $this->auth->getStorage()->read();
        
        
    }
	
    public function indexAction(){
    //
	    // === PHAN XU LY DANH CHO QUYEN GIAO VIEN
        $idLopArr = $this->manager->fetchCol(array("idLop","username = '".$this->storage->username."'"));
        $where = "";
        for($i=0;$i<count($idLopArr);$i++){
        	$where .= " id = '".$idLopArr[$i]."' or ";
        }
        $where = substr($where, 0,strlen($where)-3);

        $listLop = $this->lopTbl->listLop($where);
        
    	//
    	//thuc hien tim voi buttom tim
    	if(isset($_GET['search_box_sumit'])){
//			$listLop = $this->manager
//							->selectManager(
//									"tenLop like '%".$_GET['search_box_text']."%' and 
//									ql.username = '".$this->storage->username."'",
//									"l.tenLop"
//							);
			$listLop = $this->lopTbl
						->listLopManager(
							"tenLop like '%".$_GET['search_box_text']."%' and 
							ql.username = '".$this->storage->username."'");
    	}
	    	
    	//loc thong tin
    	if(isset($_GET['tbn_filter']) && (int)$_GET['tbn_cate'] !=0){
    		$listLop = $this->manager->selectManager("khoa = '".$_GET['tbn_cate']."' and 
														ql.username = '".$this->storage->username."'"
    					);
    	}
    	
    	//
    	// === PHAN XU LY DANH CHO QUYEN LA ADMIN
    	if($this->storage->quyen == "admin"){    		
	    	if(isset($_GET['edit'])){ //if ma yc sua
	    		$this->lopForm->addSubmitSave();
	    		$this->lopForm->addAction('edit');
	    		
	    		$this->view->titleForm = "Thay đổi thông tin";
	    		
	    		$infoEdit = $this->lopTbl->fetch($_GET['id']);
	    		$this->lopForm->addHiddenId('txtId', $infoEdit['id']);
	    						
	    		$this->lopForm->getElement('txtName')->setValue($infoEdit['tenLop']);
	    		$this->lopForm->getElement('txtKhoa')->setValue($infoEdit['khoa']);
	    	}else{
	    		$this->lopForm->setAction(Zend_Controller_Front::getInstance()->getBaseUrl().'/admin/lop/add');
	    		$this->lopForm->addSubmitAdd();
	    				
	    		$this->view->titleForm = "Thêm Lớp Mới";
	    	}
	    	
	    	//
	    	//xoa nhiu record
	    	if(isset($_GET['del']) && !empty($_GET['lop-check'])){
	    		$this->_forward('delete','delete','admin');
	    	}
	    	
	    	$this->view->lopForm = $this->lopForm;
	    	
	    	//
	    	//thuc hien tim voi buttom tim
	    	if(isset($_GET['search_box_sumit'])){
	    		
				$listLop = $this->lopTbl
								->listLop(
										"tenLop like '%".$_GET['search_box_text']."%'",
										"l.tenLop"
								);
	    	}
	    	
	    	//loc thong tin
	    	if(isset($_GET['tbn_filter']) && (int)$_GET['tbn_cate'] !=0){
	    		$listLop = $this->lopTbl
	    						->listLop("khoa = '".$_GET['tbn_cate']."'");
	    	}
	    	
	    	///default
	    	if(!isset($_GET['tbn_filter']) && !isset($_GET['search_box_sumit'])){
	    		$listLop = $this->lopTbl->listLop();
	    	}
		    	
	    }else{
	    	if(isset($_GET['edit']) || isset($_GET['del'])){
	    		$this->_redirect('/admin/index/');
	    	}
	    }
	    
    	//
    	//xu xu phan trang
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($listLop));
        $paginator->setItemCountPerPage(20)
        			->setCurrentPageNumber($this->_getParam('page','1'));//if $_GET['page'] =="" thi la 1 nguoc lai thi lay gia $_GET['page'] (tham so page la default)
        			
        $this->view->paginator = $paginator;
        
        //
        //lay khoa hoc
        $this->view->khoaHoc = $this->lopTbl->getKhoaHoc();
        
    }

    public function editAction()
    {
        if(isset($_POST['cmdEdit'])){
        	if($this->lopForm->isValid($this->_request->getPost())){
        		$name = $this->_getParam('txtName');
        		$khoa = $this->_getParam('txtKhoa');
        		$id	= $this->_getParam('txtId');
        		
        		$this->lopTbl->editLop($name, $khoa, $id);
        		
        		$this->jShowMessage("Thành công!!!!");
        		
        		$this->_forward('index','lop','admin');
        		//$this->_redirect('/admin/lop/');
        	}
        }
    }

    public function addAction()
    {
        if(isset($_POST['cmdAdd'])){
        	if($this->lopForm->isValid($this->_request->getPost())){ //if cac truong dc yeu cau deu dc nhap
        		$name 	= $this->_getParam('txtName');
	        	$khoa 	= $this->_getParam('txtKhoa');
	        	$id 	= $this->_getParam('txtId');
	        	
				$this->lopTbl->addLop($id,$name,$khoa);

				//xoa tat ca du lieu trong textbox sau khi insert thanh cong
				$this->_request->clearParams();
				
				$this->jShowMessage("Thành công!!!");
				$this->_forward('index','lop','admin');				
//				$this->_redirect('/admin/lop/');
        	}
        	$this->_redirect('/admin/lop/');
        }
        
    }
    
	public function deleteAction()
    {
    	if(isset($_GET['delete'])){
    		$this->lopTbl->deleteLop($_GET['id']);
    		
    		$this->jDialogMessage('Thông báo','Đã xóa!!!');
    		
    		$this->_forward('index','lop','admin');
    	}
    	
    	//
    	// === Xoa nhiu record
    	if(isset($_GET['del']) && !empty($_GET['lop-check'])){
    		$arr_Id = $_GET['lop-check'];
    		for($i=0; $i< count($arr_Id); $i++){
    			$this->lopTbl->deleteLop($arr_Id[$i]);
    		}
    	}
    }
    
    public function importAction(){
    	if(isset($_POST['cmdLImport'])){
    		try {
    			$fileName = WEB_ROOT.'/docs/'.$_FILES['fLImport']['name'];
	    		move_uploaded_file($_FILES['fLImport']['tmp_name'], $fileName );
	    		
	    		$dataFromExcel = $this->importFromExcel($fileName);
	    		$rowsNum = $dataFromExcel->rowcount();
	    		
	    		for($i=2; $i<=$rowsNum; $i++){
	    			$this->lopTbl->addLop(
	    					$dataFromExcel->val($i, 1),
	    					$dataFromExcel->val($i, 2),
	    					$dataFromExcel->val($i, 3)
	    				);
	    		}
	    		
	    		$this->jDialogMessage('Thông báo','Nhập thành công!!!');	
    		} catch (Exception $e) {
    			$this->jDialogMessage('Lỗi','Lỗi nhập dữ liệu. Có thể do định dạng các cột trong bảng không đúng. Vui lòng thử lại');
    		}
    		
    		$this->_forward('index','lop','admin');
    	}else if (!isset($_POST['cmdLExport'])){
    		$this->_redirect('/admin/lop/');
    	}
    }
    
    public function exportAction(){
    	if(isset($_POST['cmdLExport'])){
    		$data = $this->lopTbl->getDataLopToExport();
    		
			// === set title for column
//    		$data = $export->addRowTitle(array(
//    								'Mã',
//    								'Lớp', 
//    								'Khóa học'
//    							), $data);

//    		$data = array(
//	        1 => array ('Name', 'Surname'),
//	        array('Võ Xuân', 'Hoan'),
//	        array('Nguyễn Tấn', 'Diện')
//	        );
	    		
    		$export = $this->exportToExcel($data, array('Mã', 'Lớp','Khóa','d'));
//    		echo '<pre>';
//    		print_r($data);
//    		$export->addArray($data);
//    		$export->generateXML('Danh sach lop');

    	}
    	
   		$this->_redirect('/admin/lop/');
    }
}









